package com.ankie.demo.gateway.conf;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * @ClassName CorsConfiguration
 * @Author zhongyu
 * @Date 2020/4/25
 * @Description 跨域配置类
 */
@Configuration
public class GuliMallCorsConfiguration {

    @Bean
    public CorsWebFilter corsWebFilter() {
        // 1. 因为这里是gateway，所以配置的需要是响应下的配置
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        // 1.1 允许哪些头进行跨域
        corsConfiguration.addAllowedHeader("*");
        // 1.2 允许哪些请求方式进行跨域
        corsConfiguration.addAllowedMethod("*");
        // 1.3 允许哪些请求来源进行跨域
        corsConfiguration.addAllowedOrigin("*");
        // 1.4 允许请求携带cookie信息
        corsConfiguration.setAllowCredentials(true);
        source.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsWebFilter(source);
    }
}
